/*
Do-file name: 8_AKM_estimation.do
Author: Thiago
Date: Nov 7th 2018
This version: April 2021
Comments: This do-file exports dataset for AKM estimation in Stata and produces all figures and tables
related to Concurso data.
*/

use "$temp/judges_estimation.dta", clear

gegen mean_cases = mean(Sent_Trial_Merits_J), by(Judge_idP)
bys Judge_idP: gen mean_cases_unique = mean_cases if _n==1

egen YM_state = group(YM State)

egen crime = rowtotal(Adm_Improbity_J Crime_Public_Adm_J)
bys Judge_idP: gen crime_unique = crime>0 if _n == 1

egen impediment = rowtotal(Declared_Impediments_SuspicionsJ)
bys Judge_idP: gen impediment_unique = impediment>0 if _n == 1
gen Court_Appeal_C_mean = Court_Appeal_C/total_judges_overall
gen court_appeal_IHS = asinh(Court_Appeal_C_mean)

******************************************************************************
**Figure 1: Histogram of mean number of cases disposed on the merits by judge *
******************************************************************************

summ mean_cases_unique,d
histogram mean_cases_unique, frac graphregion(color(white)) ///
note( N = `r(N)' judges) xline(`r(p10)', lp(dash)) xline(`r(p90)', lp(dot)) ///
fcolor(dknavy) lcolor(black) lw(medthin)  ylab(, nogrid) xtitle("Mean # of Cases Disposed by Judges")
graph export "$paper_tables/histo_judge_mean.pdf", replace 

******

bys Judge_idP: keep if _n == 1												//Keep one observation per judge

keep if Concurso_enc != .													//Keep only judges with concurso grades

merge m:1 Judge_idP Concurso_enc using "$temp/judges_level_rank.dta"

*Preparing RAIS variables for analysis

gen log_wage_bef_jud = ln(avg_wage_bef_jud)
lab var log_wage_bef_jud "Log average wage before judiciary (2017 prices)"

gen age = year - birth_year
gen exp_jud = year - first_year_RAIS_jud
gen exp_rais = year - first_year_RAIS

gen age_2015 = 2015 - birth_year
gen age_2015_sqr = age_2015^2
gen experience_2015 = 2015 - first_year_RAIS
gen experience_2015_sqr = experience_2015^2
gen experience_justice_2015 = 2015 - first_year_RAIS_jud
gen experience_justice_2015_sqr = experience_justice_2015^2

gen work_before = (first_year_RAIS < first_year_RAIS_jud) if birth_year!=.

cap drop _merge

*Recover Fixed Effects from R

preserve
	import delimited "$temp/Intermed_FE_All.csv", delimiter(",") clear
	rename (judge_idp court_idp2) (Judge_idP Court_idP2)
	tempfile temp
	save "`temp'"
restore

merge m:1 Judge_idP Court_idP2 using "`temp'", keep(1 3)
drop _merge

*Recover Alternative samples Fixed Effects from R
preserve
	import delimited "$temp/Intermed_FE_Alternative.csv", delimiter(",") clear
	rename (judge_idp) (Judge_idP)
	tempfile temp
	save "`temp'"
restore

merge m:1 Judge_idP using "`temp'", keep(1 3)
drop _merge

*Recover Alternative samples Fixed Effects from R
preserve
	import delimited "$temp/Intermed_FE_Alternative_0.csv", delimiter(",") clear
	duplicates drop judge_idp, force
	rename (judge_idp) (Judge_idP)
	tempfile temp
	save "`temp'"
restore

merge m:1 Judge_idP using "`temp'", keep(1 3)
drop _merge

*Robustness FE leaving first and last months out
preserve
	import delimited "$temp/Intermed_FE_robust.csv", delimiter(",") clear
	duplicates drop judge_idp, force
	rename (judge_idp) (Judge_idP)
	tempfile temp
	save "`temp'"
restore

merge m:1 Judge_idP using "`temp'", keep(1 3)
drop _merge

*Bringing SP data
preserve
	import delimited "$temp/FE_SP_data.csv", clear
	rename (judge_idp) (Judge_idP)
	keep Judge_idP splog_fe
	tempfile temp
	save "`temp'"
restore 

merge m:1 Judge_idP using "`temp'", keep(1 3)
drop _merge


*Robustness check flexible
preserve
	import delimited "$temp/FE_newtests.csv", delimiter(",") clear
	duplicates drop judge_idp, force
	rename (judge_idp) (Judge_idP)
	tempfile temp
	save "`temp'"
restore

merge m:1 Judge_idP using "`temp'", keep(1 3)
drop _merge

*Only big Courts
preserve
	import delimited "$temp/Intermed_FE_bigCourt.csv", clear
	duplicates drop judge_idp, force
	rename (judge_idp) (Judge_idP)
	tempfile temp
	save "`temp'"
restore 

merge m:1 Judge_idP using "`temp'", keep(1 3)
drop _merge


*Standardize variables
foreach var in judge_fe judge_fe_hear judge_fe_await judge_fe_extinct judge_fe_appeal ///
	judge_fe_full judge_fe_spell4  judge_fe_total6  splog_fe ///
	 judge_fe_first2 judge_fe_firstlast2 judge_fe_bigcourt {
	bys Concurso_enc: egen `var'_sd = sd(`var')
	gen `var'_std = `var' / `var'_sd
}

foreach var in  crime impediment promotion_max {
	gen `var'_single = `var' > 0 & `var'!= . 
}

gsort Concurso_enc -judge_fe
bys Concurso_enc: gen Rank_FE_IS = _n 
*Generating quantiles of productivity
bys Concurso_enc: egen total_performance = max(Rank_FE_IS)
gen quintile_performance = .
replace quintile_performance  = 5 if (Rank_FE_IS>0.8*total_performance)
replace quintile_performance  = 4 if (Rank_FE_IS>0.6*total_performance & Rank_FE_IS<=0.8*total_performance)
replace quintile_performance  = 3 if (Rank_FE_IS>0.4*total_performance & Rank_FE_IS<=0.6*total_performance)
replace quintile_performance  = 2 if (Rank_FE_IS>0.2*total_performance & Rank_FE_IS<=0.4*total_performance)
replace quintile_performance  = 1 if (Rank_FE_IS<=0.2*total_performance)
replace quintile_performance = . if Rank_FE_IS == .

save "$data_out/judgelevel_estimation_includeFE.dta", replace		//Original final database with identified Judges and courts

***** De-identifying data for replication package *****

*Droping names and variables from RAIS 
drop Judge Court_name Munic_code3 Munic_code2 comarca gender_RAIS race birth_year educ_basic educ_hs educ_college educ_grad ///
	first_year_RAIS first_year_RAIS_public first_year_RAIS_jud first_wage_RAIS prior_wage_bef_public ///
	prior_wage_bef_jud avg_wage_bef_public avg_wage_bef_jud first_munic_RAIS first_munic_public first_munic_jud ///
	idibge ibge_res_pop idhm_renda idhm_longevidade idhm_educacao gdp_capita cars  ibge_res_pop_z ///
    idhm_renda_z idhm_longevidade_z idhm_educacao_z gdp_capita_z cars_z amenities_pca ///
	index_amenity  log_wage_bef_jud age exp_jud exp_rais age_2015 age_2015_sqr experience_2015 ///
	experience_2015_sqr experience_justice_2015 experience_justice_2015_sqr work_before

*Excluding labels that identify municipalities and courts
lab val Municipality_enc 
lab val type_court 
lab val unique_court_enc

order Judge_idP Court_idP YM

save "$data_out/judgelevel_estimation_includeFE_deident.dta", replace	




